package cn.getech.data.development.service.impl;

import cn.getech.data.development.entity.TableFieldInfo;
import cn.getech.data.development.entity.TableFieldRules;
import cn.getech.data.development.entity.TableInfo;
import cn.getech.data.development.mapper.TableFieldRulesMapper;
import cn.getech.data.development.model.vo.DataSecurityConfigVO;
import cn.getech.data.development.service.ITableFieldRulesService;
import cn.getech.data.development.service.RangerDataService;
import cn.getech.data.development.service.TableFieldInfoService;
import cn.getech.data.development.service.TableInfoService;
import cn.getech.system.center.entity.RoleRowDataPermissionConfig;
import cn.getech.system.center.mapper.RoleRowDataPermissionConfigMapper;
import cn.getech.system.center.service.SysSecurityDataLevelService;
import cn.getech.system.center.service.SysUserRoleService;
import cn.getech.system.center.utils.ShiroUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.LinkedList;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 表行规则信息 服务实现类
 * </p>
 *
 * @author hujz
 * @since 2019-12-18
 */
@Slf4j
@Service
public class TableFieldRulesServiceImpl extends ServiceImpl<TableFieldRulesMapper, TableFieldRules> implements ITableFieldRulesService {

    @Autowired
    private SysUserRoleService sysUserRoleService;
    @Autowired
    private TableInfoService tableInfoService;
    @Autowired
    private TableFieldRulesMapper tableFieldRulesMapper;
    @Autowired
    private RangerDataService rangerDataService;
    @Autowired
    private RoleRowDataPermissionConfigMapper roleRowDataPermissionConfigMapper;


    @Autowired
    private TableFieldInfoService tableFieldInfoService;
    @Autowired
    private SysSecurityDataLevelService sysSecurityDataLevelService;

    @Override
    public void deleteInfo(int tableId, int rowRoleId) {
        Long uid = ShiroUtils.getUserId();
        if (sysUserRoleService.superAdmin(uid) || tableInfoService.tableOwner(uid, tableId)) {
            TableFieldRules tableFieldRules = tableFieldRulesMapper.selectById(rowRoleId);
            if (null != tableFieldRules) {
//
//                //查询对应的配置是否关联到角色
//                List<RoleRowDataPermissionConfig> listRoleDataConfigList = roleRowDataPermissionConfigMapper.selectList(new QueryWrapper<RoleRowDataPermissionConfig>().eq("table_field_rules_id", rowRoleOd));
//
//                if (CollectionUtil.isEmpty(listRoleDataConfigList)) {
//                    //删除对应的数据
//                    tableFieldRulesMapper.deleteByDepartmentId(rowRoleId);
//                    return;
//                }

//                //并且删除对应的权限策略---直接删除这个表所有的行规则策略
//                try {
//                    TableFieldRulesDto tableFieldRulesDto = new TableFieldRulesDto();
//                    BeanUtils.copyProperties(tableFieldRules, tableFieldRulesDto);
//                    rangerDataService.deleteRangerPolicy(tableFieldRulesDto);
//                } catch (Exception e) {
//                    log.error("删除策略失败！error:{}", e.getMessage());
//                }

                //删除对应的数据
                tableFieldRulesMapper.deleteById(rowRoleId);

                //删除对应的角色关联的这个信息
                roleRowDataPermissionConfigMapper.delete(new QueryWrapper<RoleRowDataPermissionConfig>().eq("table_field_rules_id",rowRoleId));

                rangerDataService.saveRangerPolicy(tableId);

            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveAllConfig(DataSecurityConfigVO dataSecurityConfigVO) {
        Integer tableId = dataSecurityConfigVO.getId();
        Long uid = ShiroUtils.getUserId();
        if (sysUserRoleService.superAdmin(uid) || tableInfoService.tableOwner(uid, tableId)){
            Map<Integer, String> defaultTableList = sysSecurityDataLevelService.getDefaultTableList();
            Map<Integer, String> defaultColumnList = sysSecurityDataLevelService.getDefaultColumnList();

            //更新表信息
            TableInfo tableInfo = new TableInfo();
            tableInfo.setId(dataSecurityConfigVO.getId());
            tableInfo.setTableLevelId(dataSecurityConfigVO.getTableLevelId());
            String tableDataLevel = null;
            if(defaultTableList.containsKey(tableInfo.getTableLevelId()))
                tableDataLevel = defaultTableList.get(tableInfo.getTableLevelId());
            tableInfo.setSecurityDataLevel(tableDataLevel);
            tableInfoService.updateById(tableInfo);

            //更新字段信息
            List<TableFieldInfo> tableFieldInfos = new LinkedList<>();
            if (dataSecurityConfigVO.getFieldsConfig() != null) {
                for (int i = 0; i < dataSecurityConfigVO.getFieldsConfig().size(); i++) {
                    TableFieldInfo tableFieldInfo = new TableFieldInfo();
                    tableFieldInfo.setId(dataSecurityConfigVO.getFieldsConfig().get(i).getFieldId());
                    tableFieldInfo.setColcumLevelId(dataSecurityConfigVO.getFieldsConfig().get(i).getFieldLevelId());
                    String columnDataLevel = null;
                    if(defaultColumnList.containsKey(tableFieldInfo.getColcumLevelId()))
                        columnDataLevel = defaultColumnList.get(tableFieldInfo.getColcumLevelId());
                    tableFieldInfo.setSecurityDataLevel(columnDataLevel);
//                    tableFieldInfo.setEncyStyleId(dataSecurityConfigVO.getFieldsConfig().get(i).getEncyStyleId());
                    tableFieldInfos.add(tableFieldInfo);
                }
                tableFieldInfoService.updateBatchById(tableFieldInfos);
            }
        }
    }

    @Override
    public void saveEnPassword(Integer encyStyleId, TableFieldInfo tableFieldInfo) {
        tableFieldInfo.setEncyStyleId(encyStyleId);
        tableFieldInfoService.updateById(tableFieldInfo);
    }

    @Override
    public List<TableFieldRules> getTableFieldRulesList(Integer tableId){
        List<TableFieldRules> tableFieldRules = this.baseMapper.getTableFieldRulesList(tableId);
        return tableFieldRules;
    }


}
